<?php

namespace app\api\controller;

use app\common\controller\Api;

class Sms extends Api
{


    // 阿里云认证信息
    public $accessKeyId = 'LTAI5tGYZqWPoXgZGKb2yMUS';
    public $accessKeySecret = '680k9VcBc4v6JJX0T9XVllTt5pVbb8';

    function smsSend($sms)
    {
        $config = \config('alisms');
        /*if ($sms->event=='sellerlogin'){
            $template_code = $config['TemplateCode']['sellercode'];
        } else {
            $template_code = $config['TemplateCode']['code'];
        }*/

        $template_code = 'SMS_498100235';
        $param = ['store_name'=>'测试门店名称','project_name'=>'测试项目名称','date'=>'测试日期','address'=>'测试地址'];
        return $this->sendMsg($sms->mobile,$param,$template_code);
    }

    public function sendMsg($mobile,$param,$template_code)
    {
        // 短信参数配置
        $params = [
            // 系统参数
            'RegionId' => 'ap-southeast-1',      // 国际短信服务区域
            'AccessKeyId' => $this->accessKeyId,
            'Format' => 'JSON',
            'SignatureMethod' => 'HMAC-SHA1',
            'SignatureVersion' => '1.0',
            'SignatureNonce' => uniqid(),
            'Timestamp' => gmdate('Y-m-d\TH:i:s\Z'),
            'Version' => '2018-05-01',

            // 业务参数
            'Action' => 'SendMessageToGlobe',    // 国际短信接口名称
            'To' => $mobile,              // 带国家码的手机号
            'Message' => $param, // 直接填写短信内容
            'From' => 'ALIYUN'                  // 可选发信方标识
        ];

// 生成签名
        $signature = $this->createSignature($params, $this->accessKeySecret);
        $params['Signature'] = $signature;

// 发送请求
        $result = $this->sendRequest('https://dysmsapi.ap-southeast-1.aliyuncs.com', $params);

// 处理响应
        $response = json_decode($result, true);
        if(isset($response['ResponseCode']) && $response['ResponseCode'] == 'OK') {
            echo "短信发送成功，消息ID：".$response['MessageId'];
        } else {
            echo "发送失败，错误信息：".$response['Message'] ?? '未知错误';
        }

    }

    /**
     * 签名生成函数 (与之前相同)
     */
    public function createSignature($params, $accessKeySecret) {
        ksort($params);
        $queryString = '';
        foreach ($params as $key => $val) {
            $queryString .= '&' . $this->percentEncode($key) . '=' . $this->percentEncode($val);
        }
        $stringToSign = 'POST&%2F&' . $this->percentEncode(substr($queryString, 1));
        return base64_encode(hash_hmac('sha1', $stringToSign, $accessKeySecret . '&', true));
    }

    /**
     * 发送POST请求
     */
    public function sendRequest($url, $params) {
        $ch = curl_init();
        curl_setopt_array($ch, [
            CURLOPT_URL => $url,
            CURLOPT_POST => true,
            CURLOPT_POSTFIELDS => http_build_query($params),
            CURLOPT_RETURNTRANSFER => true,
            CURLOPT_TIMEOUT => 10
        ]);
        $result = curl_exec($ch);
        curl_close($ch);
        return $result;
    }

    /**
     * 特殊字符编码处理
     */
    public function percentEncode($str) {
        $res = urlencode($str);
        return str_replace(['+', '*', '%7E'], ['%20', '%2A', '~'], $res);
    }


}

?>